package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.web.controller.tool.SiteService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysContent;
import com.ruoyi.system.service.ISysContentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 内容Controller
 *
 * @author ruoyi
 * @date 2022-08-02
 */
@Controller
@RequestMapping("/system/content")
public class SysContentController extends BaseController
{
    private String prefix = "system/content";

    @Autowired
    private ISysContentService sysContentService;

    @Autowired
    private SiteService siteService;

    @RequiresPermissions("system:content:view")
    @GetMapping()
    public String content()
    {
        return prefix + "/content";
    }

    /**
     * 查询内容列表
     */
    @RequiresPermissions("system:content:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysContent sysContent)
    {
        sysContent.setSite(siteService.getSiteId().intValue());
        startPage();
        List<SysContent> list = sysContentService.selectSysContentList(sysContent);
        return getDataTable(list);
    }

    /**
     * 导出内容列表
     */
    @RequiresPermissions("system:content:export")
    @Log(title = "内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysContent sysContent)
    {
        sysContent.setSite(siteService.getSiteId().intValue());
        List<SysContent> list = sysContentService.selectSysContentList(sysContent);
        ExcelUtil<SysContent> util = new ExcelUtil<SysContent>(SysContent.class);
        return util.exportExcel(list, "内容数据");
    }

    /**
     * 新增内容
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存内容
     */
    @RequiresPermissions("system:content:add")
    @Log(title = "内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysContent sysContent)
    {
        sysContent.setSite(siteService.getSiteId().intValue());
        sysContent.setCreatedAt(new Date());
        sysContent.setUpdatedAt(new Date());
        return toAjax(sysContentService.insertSysContent(sysContent));
    }

    /**
     * 修改内容
     */
    @RequiresPermissions("system:content:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        SysContent sysContent = sysContentService.selectSysContentById(id);
        mmap.put("sysContent", sysContent);
        return prefix + "/edit";
    }

    /**
     * 修改保存内容
     */
    @RequiresPermissions("system:content:edit")
    @Log(title = "内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysContent sysContent)
    {
        sysContent.setUpdatedAt(new Date());
        return toAjax(sysContentService.updateSysContent(sysContent));
    }

    /**
     * 删除内容
     */
    @RequiresPermissions("system:content:remove")
    @Log(title = "内容", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysContentService.deleteSysContentByIds(ids));
    }
}
